﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;



using Meqs.Entity;
using Meqs.Logic.Interface;

using Castle.Services.Transaction;
using Castle.Facilities.IBatisNetIntegration;
using log4net;

namespace Meqs.Logic.Implement
{
    public class MoralityTypeLogic : CommonLogic<MoralityType, int>, IMoralityTypeLogic
    {
        private ISchoolInfoLogic _schoolLogic;
        private IMoralityItemLogic _ItemLogic;
        public MoralityTypeLogic(ISchoolInfoLogic schoolLogic, IMoralityItemLogic itemLogic)
        {
            _schoolLogic = schoolLogic;
            _ItemLogic = itemLogic;
        }
        //public MoralityTypeLogic(IMoralityItemLogic itemLogic)
        //{
        //    _ItemLogic = itemLogic;
        //}
        #region Implementation of ICommonLogic<MoralityType, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(MoralityType entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(MoralityType entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public int Delete(int entityId)
        {
            return DeleteObject(entityId);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll()
        {
            return DeleteAllObject();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public MoralityType FindById(int entityId)
        {
            return FindObjectById(entityId);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount()
        {
            return FindObjectByCount();
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<MoralityType> FindAll()
        {
            return FindAllObject();
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<MoralityType> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion

        #region Implementation of IMoralityTypeLogic

        /// <summary>
        /// 获取所有德育评价大类(小类无大类的算做大类)
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<MoralityType> GetAllMoralityType(string schoolMark)
        {
            SchoolInfo school = _schoolLogic.FindBySchoolMark(schoolMark);
            int schoolYear = school.SchoolYear;
            int term = school.SchoolTerm;
            return GetAllMoralityType(schoolMark, schoolYear, term);
        }

        /// <summary>
        /// 获取所有德育评价大类(小类无大类的算做大类)
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="schoolYear"></param>
        /// <param name="term"></param>
        /// <returns></returns>
        public IList<MoralityType> GetAllMoralityType(string schoolMark, int schoolYear, int term)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("SchoolYear", schoolYear);
            ht.Add("Term", term);
            return FindCustomer<MoralityType>("MoralityType.GetAllMoralityType", ht);
        }

        public IList<MoralityType> FindAllType(string schoolMark, int schoolYear, int term, int historyID)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("schoolYear", schoolYear);
            ht.Add("term", term);
            ht.Add("HistoryID", historyID);
            return FindCustomer<MoralityType>("MoralityType.FindAllType", ht);
        }
             
        /// <summary>
        /// 添加类型
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="historyId"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public int InsertType(string schoolMark, int historyId, string typeName)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("historyId", historyId);
            ht.Add("typeName", typeName);

            return (int)InsertCustomer("MoralityType.InsertType", ht);
        }

        /// <summary>
        /// 添加history和type
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="typeName"></param>
        /// <param name="schoolYear"></param>
        /// <param name="term"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public int InsertTypeHistory(string schoolMark,string typeName,int schoolYear, int term, int userId)
        {
            int n = 0;
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("SchoolYear", schoolYear);
            table.Add("Term", term);
            table.Add("Status", 0);
            table.Add("ManagerID", userId);

            int historyId = (int)InsertCustomer("MoralityHistory.InsertNew", table);
            if (historyId > 0)
            {
                n = InsertType(schoolMark, historyId, typeName);
            } 
            return n;
        }

        /// <summary>
        /// 验证学年学期下的类型名是否重复
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="schoolYear"></param>
        /// <param name="term"></param>
        /// <param name="typeName"></param>
        /// <returns>0-不重复，大于0-重复</returns>
        public int ValidateTypeName(string schoolMark, int schoolYear, int term, string typeName)
        {
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("schoolYear", schoolYear);
            table.Add("term", term);
            table.Add("typeName", typeName);

            return FindCustomerByCount("MoralityType.ValidateTypeName", table);
        }


        /// <summary>
        /// 删除类型
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="rid"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.Requires)]
        public int DeleteById(string schoolMark, int rid)
        {
            int n = 0;
            //先删除item
            n = _ItemLogic.DeleteByTypeId(schoolMark, rid);
 
            //再删除type
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("rid", rid);

            n = DeleteCustomer("MoralityType.DeleteById", table);
            return n;
        }

        /// <summary>
        /// 更新类型名
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="rid"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public int UpdateTypeName(string schoolMark, int rid, string typeName)
        {
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("rid", rid);
            table.Add("typeName", typeName);

            return UpdateCustomer("MoralityType.UpdateTypeName", table);
        }

        public IList<MoralityType> GetTypesByKeyWord(string schoolMark, int schoolYear, int term, string keyWord)
        {
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("SchoolYear", schoolYear);
            table.Add("Term", term);
            table.Add("KeyWord", keyWord);
            return FindCustomer<MoralityType>("MoralityType.GetTypesByKeyWord", table);
        }
        #endregion
    }
}